<?php
namespace app\index\controller;

use think\Controller;
use think\Response\Json;
use think\Db;

/**
* 前台首页
*/
class Index extends Base
{	
	public function _initialize() {
		parent::_initialize();
	}

	
    public function index()
    {
    	$map['is_recommend']       = 1;
		$posts 					   = Db::name('content')->where($map)->order('rand()')->limit(8)->select();
		$map1['a.is_recommend']    = 1;
		$map1['b.cat_name'] 	   = '首页';

		$recommends 			   = $this->normal(Db::name('slide')->alias('a')->join('web_slide_cat b', 'b.id=a.slide_cid')->where($map1)->limit(3)->select(), true);
		$recommend 				   = $recommends[0];

		
		$this->assign('recommends', $recommends);
		$this->assign('recommend', $recommend);
		$this->assign('posts', $posts);

    	return $this->fetch('Index/index');
    }

    // 正常化recommend slide
    public function normal($d, $f = true) {
    	if($f) {
    		foreach($d as $k => &$v) {
	    		$v['title'] 			= $v['slide_name'];
	    		$v['desc'] 				= $v['slide_des'];
	    		$v['img'] 				= $v['slide_url'];
	    		$v['time']              = date('H-m-d', $v['time']);
	     	}
	     } else {
	     	$d['title'] 				= $d['slide_name'];
	     	$d['desc'] 					= $d['slide_des'];
	     	$d['img'] 					= $d['slide_url'];
            $d['time']                  = date('H-m-d', $d['time']);
	     }
    	

    	return $d;
    }
}
